package com.fjwt.gz.mgr.ctrl.award.coupon;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardCouponDTO;
import com.fjwt.gz.db.vo.AwardCouponVO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/***
 * 卡券奖品管理 Controller
 *
 */
@RestController
@Slf4j
@RequestMapping("/api/award/coupon")
public class AwardCouponController extends CommonCtrl {

    @Autowired
    private AwardCouponService awardCouponService;


    /**
     * 查询卡券奖品列表
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO')")
    @GetMapping
    public ApiRes list() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端查询卡券奖品列表，请求地址：/api/award/coupon，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        IPage<AwardCouponVO> page = awardCouponService.queryList(getIPage(true), awardCouponDTO);
        log.info("pc端查询卡券奖品列表，请求地址：/api/award/coupon，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 查询卡券奖品详情
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_VIEW')")
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ApiRes details() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端查询卡券奖品详情，请求地址：/api/award/coupon/details，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        AwardCouponVO vo = awardCouponService.detail(awardCouponDTO);
        log.info("pc端查询卡券奖品详情，请求地址：/api/award/coupon/details，返回结果：{}", JSONUtil.toJsonStr(vo));
        AwardCouponVO data = new AwardCouponVO();
        BeanUtils.copyProperties(vo, data);
        return ApiRes.ok(data);
    }

    /**
     * 新增卡券奖品
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_ADD', 'ENT_ACTIVITY_LIST_ADD')")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ApiRes add() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        awardCouponDTO.setCreatedUid(getCurrentUser().getSysUserId());
        log.info("pc端新增卡券奖品，请求地址：/api/award/coupon，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        AwardCouponVO awardCouponVO = awardCouponService.add(awardCouponDTO);
        log.info("pc端新增卡券奖品，请求地址：/api/award/coupon，返回结果：{}", JSONUtil.toJsonStr(awardCouponVO));
        return ApiRes.ok(awardCouponVO);
    }

    /**
     * 修改卡券奖品详情
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_EDIT')")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ApiRes update() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端修改卡券奖品详情，请求地址：/api/award/coupon，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        awardCouponService.update(awardCouponDTO);
        return ApiRes.ok();
    }

    /**
     * 更改卡券奖品状态
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_EDIT', 'ENT_ACTIVITY_LIST_ADD')")
    @RequestMapping(value = "/state", method = RequestMethod.PUT)
    public ApiRes updateState() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端修改卡券奖品状态，请求地址：/api/award/coupon/state，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        awardCouponService.updateState(awardCouponDTO);
        return ApiRes.ok();
    }

    /**
     * 修改卡券状态为草稿状态
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_DRAFT')")
    @RequestMapping(value = "/updateToDraft", method = RequestMethod.PUT)
    public ApiRes updateCouponStateToDraft() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端修改卡券奖品状态为草稿状态，请求地址：/api/award/coupon/updateToDraft，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        awardCouponService.updateCouponStateToDraft(awardCouponDTO);
        return ApiRes.ok();
    }

    /**
     * 删除卡券奖品
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_DEL')")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ApiRes delete() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端删除卡券奖品，请求地址：/api/award/coupon，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        awardCouponService.delete(awardCouponDTO);
        return ApiRes.ok();
    }

    /**
     * 随机减券
     * 满减券
     * 新增
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_ADD')")
    @PostMapping("/saveCoupon")
    @Transactional(rollbackFor = Exception.class)
    public ApiRes saveCoupon(){
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        // 当前登录用户信息
        awardCouponDTO.setCreatedUid(getCurrentUser().getSysUserId());
        log.info("pc端新增卡券奖品，请求地址：/api/award/coupon/saveCoupon，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        return ApiRes.ok(awardCouponService.saveCoupon(awardCouponDTO));
    }


    /**
     * 随机减券
     * 满减券
     * 修改
     */
    @PreAuthorize("hasAnyAuthority('ENT_COUPON_INFO_EDIT')")
    @PostMapping("/editCoupon")
    @Transactional(rollbackFor = Exception.class)
    public ApiRes editCoupon(){
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端修改卡券奖品，请求地址：/api/award/coupon/editCoupon，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        awardCouponService.editCoupon(awardCouponDTO);
        return ApiRes.ok();
    }

    /**
     * 获取未分配活动的卡券奖品列表
     * @return
     */
    @RequestMapping(value = "/usable", method = RequestMethod.GET)
    public ApiRes listWithoutActivity() {
        AwardCouponDTO awardCouponDTO = getObject(AwardCouponDTO.class);
        log.info("pc端查询卡券奖品详情，请求地址：/api/award/coupon/usable，请求参数：{}", JSONUtil.toJsonStr(awardCouponDTO));
        List<AwardCouponVO> vo = awardCouponService.queryList(awardCouponDTO);
        log.info("pc端查询卡券奖品详情，请求地址：/api/award/coupon/usable，返回结果：{}", JSONUtil.toJsonStr(vo));
        return ApiRes.ok(vo);
    }
}
